app.factory("connectAdmin", ["$http", "$window", "$q", "$location", "$exception", "$token", "$canceler", function ($http, $window, $q, $location, $exception, $token, $canceler) {
    function ConnectAdminService() {
    };

    ConnectAdminService.prototype = {
        connect: function (model, shopId/*optional*/) {
            return $http.post("/admin/connect", model, { timeout: $canceler.create().promise }).success(function (res) {
                return $token.set(res.token).then(function () {
                    $location.path(res.route);
                });
            }).catch(function (error) {
                return $exception.show(error);
            });
        },
        disconnect: function () {
            return $token.reset().finally(function(){
                $window.location = "/admin";
            });
        }
    };
    return new ConnectAdminService();
} ]);